/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;


import POJO.NamHocPOJO;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 * @author
 *  ITEC.HCMUS.EDU.VN
 *  Ngô Huy Đạt
 *  0869005
 *  nhdat.itec@gmail.com
 */
public class NamHocDAO {

    public static ArrayList<NamHocPOJO> selectAll(){
        ArrayList<NamHocPOJO> list = new ArrayList<NamHocPOJO>();
        try{
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.open();
            
            String sql = "SELECT * FROM DSNAMHOC";
            ResultSet rs = helper.excecuteQuery(sql);
            while(rs.next()){
                NamHocPOJO n = new NamHocPOJO();
                n.setMaNamHoc(rs.getString("MaNamHoc"));
                n.setNamHoc(rs.getString("NamHoc"));
                list.add(n);
            }

            helper.close();
        }catch(Exception ex){
            System.out.print(ex.getMessage());
        }
        return list;
    }


    public static boolean insert(String MaNamHoc,String NamHoc){
        boolean kq = false;
        try{
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.open();

            String sql = String.format("INSERT INTO DSNAMHOC(MaNamHoc,NamHoc) VALUES('%s','%s')",MaNamHoc,NamHoc);
            int num = helper.excecuteUpdate(sql);
            if(num==1)  kq = true;

            helper.close();
        }catch(Exception ex){
            System.out.print(ex.getMessage());
        }
        return kq;
    }
    
    
    public static boolean  delete(String MaNamHoc) {
        boolean kq = false;
        try {
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.open();

            String sql = String.format("DELETE FROM DSNAMHOC WHERE MaNamHoc = '%s'",MaNamHoc);
            int num = helper.excecuteUpdate(sql);
            if(num==1)  kq = true;
            
            helper.close();
        } catch (Exception ex) {
            System.out.print(ex.getMessage());
        }
        return kq;
    }


    public static boolean  update(String MaNamHoc,String NamHoc) {
        boolean kq = false;
        try {
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.open();
            
            String sql = String.format("UPDATE DSNAMHOC SET NamHoc='%s' WHERE MaNamHoc='%s'",NamHoc,MaNamHoc);
            int num = helper.excecuteUpdate(sql);
            if(num==1)  kq = true;

            helper.close();
        } catch (Exception ex) {
            System.out.print(ex.getMessage());
        }
        return kq;
    }

    //END
}
